package org.karelun.gef.actions;

import org.eclipse.gef.ui.actions.SelectionAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchPart;
import org.karelun.gef.commands.CreateAttributeCommand;
import org.karelun.gef.model.BaseClassModel;
import org.karelun.gef.model.ClassModel;
import org.karelun.gef.parts.NodePart;

public abstract class AbstractAddAttributeAction extends SelectionAction{
	
	protected ClassModel classModel=null;
	
	public AbstractAddAttributeAction(IWorkbenchPart part) {
		super(part);
	}

	protected boolean calculateEnabled() {
		ISelection s = getSelection();
		if(s instanceof IStructuredSelection){
			Object obj = ((IStructuredSelection)s).getFirstElement();
			if(obj instanceof NodePart){
				Object model = ((NodePart)obj).getModel();
				if(model instanceof ClassModel){
					classModel=(ClassModel)model;
					return true;
				}
			}
		}
		return false;
	}
	
	public void run() {
		if(classModel==null)
			return;
		CreateAttributeCommand command = new CreateAttributeCommand();
		command.setClassModel(classModel);
		command.setAttribute(createAttrModel());
		command.execute();
	}

	protected abstract BaseClassModel createAttrModel();
	
}
